package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.dto.ParamDTO;
import com.etone.smartAudit.service.AuditExceptionService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.auditException.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author: 蒋学伟
 * @create: 2020-06-10 15:32:10
 * @Description AuditExceptionController
 */
@AllArgsConstructor
@RestController
@Api(tags = "审计异常", value = "AuditExceptionController")
public class AuditExceptionMonthController {

    private AuditExceptionService auditExceptionService;

    @ApiOperation(value = "获取已有月份列表《完》")
    @GetMapping("/auditExceptions/months")
    public PagerVO<List<Integer>> findAuditExceptionMonth(
            @ApiParam(value = "审计点id") @RequestParam(required = false) Integer auditExceptionTypeId,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return auditExceptionService.findAuditExceptionMonth(auditExceptionTypeId, page, pageSize);
    }

    @ApiOperation(value = "查询最近一个月全省的数据《完》")
    @GetMapping("/auditExceptions/areas/total")
    public AuditExceptionMonthAreaListVO findLastMonthArea(
            @ApiIgnore @ApiParam(value = "可以不填写") @RequestParam(defaultValue = "10025", required = false) String areaId,
            @ApiParam(value = "typeId") @RequestParam(required = false) String typeId
    ) {
        return auditExceptionService.findLastMonthArea(areaId, typeId);
    }

    @ApiOperation(value = "查询区域专题数《完》")
    @GetMapping("/auditExceptions/areas/special/total")
    public List<AuditExceptionSpecialAreaListVO> findAreaSpecialTotal(
            @ApiParam(value = "0:集团,10025全省，默认全省") @RequestParam(defaultValue = "10025") String areaId,
            @ApiParam(value = "月份yyyyMM，例:202009") @RequestParam Integer date
    ) {
        return auditExceptionService.findAreaSpecialTotal(areaId, date);
    }

    @ApiOperation(value = "查询区域专题详情《完》")
    @GetMapping("/auditExceptions/areas/special/detail")
    public List<AuditExceptionSpecialDetailVO> findAreaSpecialDetail(
            @ApiParam(value = "0:集团,10025全省，默认全省") @RequestParam(defaultValue = "10025") String areaId,
            @ApiParam(value = "城市id") @RequestParam String cityId,
            @ApiParam(value = "月份yyyyMM，例:202009") @RequestParam Integer date
    ) {
        return auditExceptionService.findAreaSpecialDetail(areaId, cityId, date);
    }

    @ApiOperation(value = "查询专题top《完》")
    @GetMapping("/auditExceptions/special/top/{top}")
    public List<String> findSpecialTop(
            @ApiParam(value = "top", required = true) @PathVariable Integer top,
            @ApiParam(value = "0:集团,10025全省，默认全省") @RequestParam(defaultValue = "10025") String areaId,
            @ApiParam(value = "月份yyyyMM，例:202009") @RequestParam Integer date
    ) {
        return auditExceptionService.findSpecialTop(top, areaId, date);
    }

    @ApiOperation(value = "查询列表《完》")
    @GetMapping("/auditExceptions")
    public PagerVO<AuditExceptionMonthVO> list(
            @ApiParam(value = "时间（格式为：202007）") @RequestParam(required = false) String date,
            @ApiParam(value = "地市id") @RequestParam(required = false) Integer cityId,
            @ApiParam(value = "类型id") @RequestParam(required = false) Integer typeId,
            @ApiParam(value = "专题id") @RequestParam(required = false) Integer specialId,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return auditExceptionService.list(date, cityId, typeId, specialId, page, pageSize);
    }

    @ApiOperation(value = "审计异常详情《完》")
    @PostMapping("/auditExceptions/detail")
    public AuditExceptionDetailVO detail(
            @ApiParam(value = "审计点id") @RequestParam Integer typeId,
            @ApiParam(value = "地市名称") @RequestParam(required = false) List<String> cityNames,
            @ApiParam(value = "月份，格式yyyyMM") @RequestParam(required = false) List<String> dates,
            @ApiParam(value = "筛选") @RequestBody(required = false) List<ParamDTO> params,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return auditExceptionService.getAuditExceptionDetailVO(typeId, cityNames, dates, params, pageSize, page);
    }

    @ApiOperation(value = "查询所有异常类型，用于下拉选择《完》")
    @GetMapping("/auditExceptions/types/simple")
    public List<AuditExceptionTypeSimpleVO> findTypeSimple(
            @ApiParam(value = "专题id") @RequestParam(required = false) Integer specialId
    ) {
        return auditExceptionService.findTypeSimple(specialId);
    }

    @ApiOperation(value = "查询所有异常类型，用于列表《完》")
    @GetMapping("/auditExceptions/types")
    public PagerVO<AuditExceptionTypeVO> findTypes(
            @ApiParam(value = "专题id") @RequestParam(required = false) Integer specialId,
            @ApiParam(value = "审计点id") @RequestParam(required = false) Integer typeId,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return auditExceptionService.findTypes(specialId, typeId, page, pageSize);
    }

    @AdminOperation(module = "持续审计", item = "下载")
    @ApiOperation(value = "下载审计异常《完》")
    @GetMapping("/auditExceptions/download/{id}")
    public void download(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            HttpServletResponse response
    ) {
        auditExceptionService.download(Arrays.asList(id), response);
    }

    @AdminOperation(module = "持续审计", item = "批量下载")
    @ApiOperation(value = "批量下载审计异常《完》")
    @GetMapping("/auditExceptions/batchDownload/{ids}")
    public void batchDownload(
            @ApiParam(value = "ids", required = true) @PathVariable List<Integer> ids,
            HttpServletResponse response
    ) {
        auditExceptionService.download(ids, response);
    }

    @ApiOperation(value = "更新文件《完》")
    @GetMapping("/auditExceptions/createFile")
    public void createLocalFile(
            @ApiParam(value = "是否强制更新") @RequestParam(required = false, defaultValue = "false") boolean force
    ) {
        auditExceptionService.createLocalFile(force);
    }

    @ApiOperation(value = "统计报表")
    @PostMapping("/auditExceptions/statisticalReport")
    public Map<String, Object> statisticalReport(
            @ApiParam(value = "审计点id") @RequestParam(required = false) Integer typeId,
            @ApiParam(value = "参数") @RequestBody(required = false) Map<String, List<ParamDTO>> actions
    ) {
        return auditExceptionService.statisticalReport(typeId, actions);
    }

    @ApiOperation(value = "手动统计异常报表")
    @GetMapping("/auditExceptions/countReport")
    public void countReport() {
        auditExceptionService.auditExceptionCountReport();
    }

}
